package array;

import java.util.Arrays;

public class L209 {

    public static int minSubArrayLen(int target, int[] nums) {
       /* int result=0;
        int minlen=Integer.MAX_VALUE;
        for (int i=0;i<nums.length;i++){
            int sum=0;
            for (int j=i;j<nums.length;j++){
                sum=sum+nums[j];
                if (sum>=target){
                    minlen=Math.min(minlen,j-i+1);

                }
            }

        }
        result=minlen==Integer.MAX_VALUE?0:minlen;
        return result;*/
        int result=0;
        int minlen=Integer.MAX_VALUE;
        int i=0;
        int sum=0;
        for (int j=0;j<nums.length;j++){
            sum+=nums[j];
            while (sum>=target){
                int sublen=j-i+1;
                minlen=Math.min(minlen,sublen);
                sum=sum-nums[i];
                i++;

            }

        }
        result=minlen==Integer.MAX_VALUE?0:minlen;
        return result;


    }
    public static void main(String[] args) {

        int target = 7;
        int[] nums = {2,3,1,2,4,3};

        int result = minSubArrayLen(target, nums);
        System.out.println(result);
    }
}
